package com.yuerlive.paperplane;


import android.content.Intent;
import android.content.res.Configuration;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yuerlive.paperplane.module.main.FavoritesFragment;
import com.yuerlive.paperplane.module.main.InfoFragment;
import com.yuerlive.paperplane.module.main.TimeLineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    private static final String KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID = "KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID";
    @BindView(R.id.flContainer)
    FrameLayout flContainer;
    @BindView(R.id.bnv)
    BottomNavigationView bnv;
    private TimeLineFragment timeLineFragment;
    private InfoFragment infoFragment;
    private FavoritesFragment favoritesFragment;
    public static final String ACTION_FAVORITES = "com.yuerlive.paperplane.favorite";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG,"onCreate"+savedInstanceState);
       // Log.e(TAG,"onSaveInstanceState"+savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragments(savedInstanceState);
        if(savedInstanceState!=null){
            int id=savedInstanceState.getInt(KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID,R.id.nav_timeline);
            switch (id){
                case R.id.nav_timeline:
                    showFragment(timeLineFragment);
                    break;
                case R.id.bnv:
                    showFragment(infoFragment);
                    break;
                case R.id.nav_favorites:
                    showFragment(favoritesFragment);
                    break;
            }
        }else {
            if (getIntent().getAction().equals(ACTION_FAVORITES)) {
                showFragment(favoritesFragment);
                bnv.setSelectedItemId(R.id.nav_favorites);
            } else {
                showFragment(timeLineFragment);
            }
        }
        bnv.setOnNavigationItemSelectedListener((menuItem->{
            FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
            switch (menuItem.getItemId()){
                case R.id.nav_timeline:
                    showFragment(timeLineFragment);
                    break;
                case R.id.nav_favorites:
                    showFragment(favoritesFragment);
                    break;
                case R.id.nav_about:
                    showFragment(infoFragment);
                    break;
            }
            ft.commit();
            return  true;
        }));
    }

    private void showFragment(Fragment fragment) {
        FragmentManager fm=getSupportFragmentManager();
        if(fragment instanceof TimeLineFragment){
            fm.beginTransaction().show(timeLineFragment).hide(infoFragment).
                    hide(favoritesFragment).commit();
        }else if(fragment instanceof  InfoFragment){
            fm.beginTransaction().show(infoFragment).hide(timeLineFragment).
                    hide(favoritesFragment).commit();
        }else if(fragment instanceof  FavoritesFragment){
            fm.beginTransaction().show(favoritesFragment).hide(timeLineFragment)
                    .hide(infoFragment).commit();
        }
    }

    private void initFragments(Bundle savedInstanceState) {
        FragmentManager fm=getSupportFragmentManager();
        if(savedInstanceState==null){
            timeLineFragment = TimeLineFragment.newInstance();
            infoFragment = InfoFragment.newInstance();
            favoritesFragment = FavoritesFragment.newInstance();
        }else{
            timeLineFragment=(TimeLineFragment) fm.getFragment(savedInstanceState,TimeLineFragment.class.getSimpleName());
            infoFragment=(InfoFragment)fm.getFragment(savedInstanceState,InfoFragment.class.getSimpleName());
            favoritesFragment=(FavoritesFragment)fm.getFragment(savedInstanceState,FavoritesFragment.class.getSimpleName());
        }
        if(!timeLineFragment.isAdded()){
            fm.beginTransaction().add(R.id.flContainer,timeLineFragment,TimeLineFragment.class.getSimpleName()).commit();
        }
        if(!infoFragment.isAdded()){
            fm.beginTransaction().add(R.id.flContainer,infoFragment,InfoFragment.class.getSimpleName()).commit();
        }
        if(!favoritesFragment.isAdded()){
            fm.beginTransaction().add(R.id.flContainer,favoritesFragment,FavoritesFragment.class.getSimpleName()).commit();
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG,"onRestart");
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        Log.e(TAG,"onContentChanged");
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        Log.e(TAG,"onPostCreate");
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.e(TAG,"onPostResume");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e(TAG,"onConfigurationChanged");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG,"onNewIntent");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG,"onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG,"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG,"onDestroy");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e(TAG,"onSaveInstanceState111111111111111");
        outState.putInt(KEY_BOTTOM_NAVIGATION_VIEW_SELECTED_ID,bnv.getSelectedItemId());
        FragmentManager fm=getSupportFragmentManager();
        if(timeLineFragment.isAdded()){
            fm.putFragment(outState,TimeLineFragment.class.getSimpleName(),timeLineFragment);
        }
        if(favoritesFragment.isAdded()){
            fm.putFragment(outState,FavoritesFragment.class.getSimpleName(),favoritesFragment);
        }
        if(infoFragment.isAdded()){
            fm.putFragment(outState,InfoFragment.class.getSimpleName(),infoFragment);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.e(TAG,"onSaveInstanceState");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e(TAG,"onRestoreInstanceState");
    }
}
